﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharedGenomics.TextMining
{
    /// <summary>
    /// This class represents a section of a string tiling - an input string is split into a sequence of term and non-term sections {s1}{s2}...{sN} by the 
    /// <see cref="StringTiler"/>. If a section is a term, then it will have a c-value score and this score will be placed in a particular quartile compared 
    /// with all the other terms found in the collection used by the tiling algorithm.
    /// </summary>
    public class TileSection
    {
        #region Private Members

        private bool _isTerm;
        private string _sectionText;
        private int _quartile;
        private double _cValue;

        #endregion

        #region Constructors

        public TileSection(string sectionText)
        {
            this.Text = sectionText;
            this.Quartile = 1;
            this.IsTerm = false;
        }

        public TileSection(ScoredTerm term, int quartile)
        {
            if (term == null)
                throw new ArgumentNullException("term");

            this.IsTerm = true;
            this.Text = term.Term;
            this.CValue = term.CValue;
            this.Quartile = quartile;
        }

        #endregion

        #region Properties

        public string Text
        {
            get { return this._sectionText; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("Text");

                if (value == String.Empty)
                    throw new ArgumentException("Text cannot be empty");

                this._sectionText = value;
            }
        }

        public int WordCount
        {
            get { return this.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length; }
        }

        public int Quartile
        {
            get { return this._quartile; }
            private set
            {
                //quartile must be in the range [1, 4]
                if (value < 1 || value > 4)
                    throw new ArgumentOutOfRangeException("Quartile must be in the range [1, 4]");

                this._quartile = value;
            }
        }

        public bool IsTerm
        {
            get { return this._isTerm; }
            private set { this._isTerm = value; }
        }

        public double CValue
        {
            get
            {
                if (!this.IsTerm)
                    throw new InvalidOperationException("Cannot fetch a c-value score for non-terms");

                return this._cValue;
            }
            private set { this._cValue = value; }
        }

        #endregion
    }
}
